/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.generator.proxy;

/**
 *
 * @author Laurent WOUTERS
 */
public class InstanceWriter {
    public static void write(java.io.Writer writer, xowl.generator.InstanceGenerator inst) throws java.io.IOException {
        writer.write("    // <editor-fold defaultstate=\"collapsed\" desc=\"Static instance " + inst.getName() + "\">\n");
        writer.write("    private static " + inst.getType().getJavaName() + " p_" + inst.getName() + ";\n");
        writer.write("    public static " + inst.getType().getJavaName() + " get_" + inst.getName() + "(xowl.interpreter.Repository repository) {\n");
        writer.write("        if (p_" + inst.getName() + " != null) return p_" + inst.getName() + ";\n");
        writer.write("        p_" + inst.getName() + " = get(repository, \"" + inst.getIRI() + "\");\n");

        for (xowl.lang.runtime.PropertyAssertion assertion : inst.getOWLInstance().getAllAsserts()) {
            if (assertion instanceof xowl.lang.runtime.ObjectPropertyAssertion) {
                xowl.lang.runtime.ObjectPropertyAssertion a = (xowl.lang.runtime.ObjectPropertyAssertion)assertion;
                xowl.generator.PropertyImplementation prop = inst.getType().getPropertyImplementation(a.getPropertyAs(null));
                xowl.generator.InstanceGenerator value = inst.getType().getPackage().getModel().getInstanceGenerator((xowl.lang.runtime.NamedIndividual)a.getValueIndividual());
                String property = prop.getProperty().getName();
                property = String.valueOf(property.charAt(0)).toUpperCase() + property.substring(1);
                writer.write("        p_" + inst.getName() + ".");
                if (prop.isVector())
                    writer.write("add");
                else
                    writer.write("set");
                writer.write(property + "(");
                if (value.getType().getJavaName().equals("java.lang.Class")) {
                    String className = inst.getType().getPackage().getModel().getBasePackage() + ".";
                    className +=  inst.getType().getPackage().getModel().getPackage(value.getOWLInstance().getInterpretationOf().getContainedBy()).getName() + ".";
                    className +=  value.getName();
                    writer.write(className + ".class");
                } else {
                    writer.write(value.getType().getJavaName() + ".get_" + value.getName() + "()");
                }
                writer.write(");\n");
            } else {
                xowl.lang.runtime.DataPropertyAssertion a = (xowl.lang.runtime.DataPropertyAssertion)assertion;
                xowl.generator.PropertyImplementation prop = inst.getType().getPropertyImplementation(a.getPropertyAs(null));
                xowl.lang.runtime.Literal value = a.getValueLiteral();
                xowl.generator.JavaDatatype datatype = prop.getProperty().getJavaDatatype(value.getMemberOf());
                String property = prop.getProperty().getName();
                property = String.valueOf(property.charAt(0)).toUpperCase() + property.substring(1);
                String data = datatype.getToValue("\"" + value.getLexicalValue() + "\"");
                writer.write("        p_" + inst.getName() + ".");
                if (prop.isVector())
                    writer.write("add");
                else
                    writer.write("set");
                writer.write(property + "(");
                writer.write(data);
                writer.write(");\n");
            }
        }
        
        writer.write("        return p_" + inst.getName() + ";\n");
        writer.write("    }\n");
        writer.write("    // </editor-fold>\n");
    }
}
